package com.tz.fruit.controller.backstage;

import com.tz.fruit.entity.Category;
import com.tz.fruit.entity.PageBean;
import com.tz.fruit.entity.Product;
import com.tz.fruit.entity.RecState;
import com.tz.fruit.service.IProductService;
import com.tz.fruit.validation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller(value = "backCategoryController")
@RequestMapping("/backstage/category")
public class CategoryController {

    @Autowired
    IProductService productService;

    /**
     * 查询符合条件的商品分类
     */
    @RequestMapping("/list")
    public String list(Model model, String categoryNo, String categoryName) {
        List<Category> categories = productService.findAllCategory(categoryNo,categoryName);
        model.addAttribute("categories", categories);
        model.addAttribute("categoryNo", categoryNo);
        model.addAttribute("categoryName", categoryName);
        return "/backstage/category/list";
    }

    /**
     * 跳转到编辑页面
     */
    @RequestMapping("/editPre")
    public String editPre(Model model, Long categoryId) {
        //根据Id查询商品分类
        Category category = productService.findByCategoryId(categoryId);
        //把对象放入request作用域
        model.addAttribute("category", category);
        return "/backstage/category/edit";
    }

    /**
     * 更新商品分类
     */
    @RequestMapping("/edit")
    public String edit(Category category){
        productService.update(category);
        return "redirect:list";
    }

    /**
     * 删除商品分类
     */
    @RequestMapping("/delete")
    public String delete(Long[] categoryId) {
        for (int i = 0; i < categoryId.length; i++) {
            productService.delByCategoryId(categoryId[i]);
        }
        return "redirect:list";
    }

    /**
     * 跳转到添加页面
     */
    @RequestMapping("/addPre")
    public String addPre() {
        return "/backstage/category/add";
    }

    /**
     * 添加商品分类
     */
    @RequestMapping("/add")
//    public String add(Model model, @Validated(value = {Group1.class, Group2.class}) @ModelAttribute Category category, BindingResult bindingResult, HttpServletRequest req, MultipartFile file) throws IOException {
    public String add(Category category) {
        productService.save(category);
        return "redirect:list";
    }

    /**
     * 检查分类编号以及分类名称是否已经存在
     */
    @RequestMapping("/checkData")
    public String checkData(String categoryNo, String categoryName, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        if (categoryNo != null && categoryNo.trim().length() > 0 && categoryName != null && categoryName.trim().length() > 0) {
            String result = "";
            //检查编号
            if (productService.findByCategoryNo(categoryNo) == null) {
                result += "true";
            } else {
                result += "false";
            }
            //检查名称
            if (productService.findByCategoryName(categoryName) == null) {
                result += ":true";
            } else {
                result += ":false";
            }
            out.print(result);
        }
        return null;
    }


    /**
     * 检查分类编号是否已经存在
     */
    @RequestMapping("/checkCategoryNo")
    public String checkCategoryNo(String categoryNo, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        if (categoryNo != null && categoryNo.trim().length() > 0) {
            //检查编号
            if (productService.findByCategoryNo(categoryNo) == null) {
                out.print("true");
            } else {
                out.print("false");
            }
        }
        return null;
    }


    /**
     * 检查分类名称是否已经存在
     *
     * @param exclude 原来的categoryName，排除
     */
    @RequestMapping("/checkCategoryName")
    public String checkCategoryName(String categoryName, String exclude, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        if (categoryName != null && categoryName.trim().length() > 0) {
            Category category = productService.findByCategoryName(categoryName);
            if (category == null) {
                out.print("true");
            } else if (null != exclude && exclude.trim().length() > 0) {
                if (exclude.equals(category.getCategoryName())) {
                    out.print("true");
                } else {
                    out.print("false");
                }
            } else {
                out.print("false");
            }
        }
        return null;
    }


}
